44dd227381c2037dbc64b069bcb9adb87f24793a
3 * Implements Special:Whatlinkshere
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @todo Use some variant of Pager or something; the pagination here is lousy.
25 * Implements Special:Whatlinkshere
27 * @ingroup SpecialPage
29 class SpecialWhatLinksHere
extends SpecialPage
{
32 protected $opts, $target, $selfTitle;
34 protected $limits = array( 20, 50, 100, 250, 500 );
36 public function __construct() {
37 parent
::__construct( 'Whatlinkshere' );
40 function execute( $par ) {
41 $out = $this->getOutput();
45 $opts = new FormOptions();
47 $opts->add( 'target', '' );
48 $opts->add( 'namespace', '', FormOptions
::INTNULL
);
49 $opts->add( 'limit', 50 );
50 $opts->add( 'from', 0 );
51 $opts->add( 'back', 0 );
52 $opts->add( 'hideredirs', false );
53 $opts->add( 'hidetrans', false );
54 $opts->add( 'hidelinks', false );
55 $opts->add( 'hideimages', false );
57 $opts->fetchValuesFromRequest( $this->getRequest() );
58 $opts->validateIntBounds( 'limit', 0, 5000 );
60 // Give precedence to subpage syntax
62 $opts->setValue( 'target', $par );
65 // Bind to member variable
68 $this->target
= Title
::newFromURL( $opts->getValue( 'target' ) );
69 if( !$this->target
) {
70 $out->addHTML( $this->whatlinkshereForm() );
74 $this->getSkin()->setRelevantTitle( $this->target
);
77 $this->selfTitle
= $this->getTitle( $this->target
->getPrefixedDBkey() );
79 $out->setPageTitle( wfMsg( 'whatlinkshere-title', $this->target
->getPrefixedText() ) );
80 $out->setSubtitle( wfMsg( 'whatlinkshere-backlink', Linker
::link( $this->target
, $this->target
->getPrefixedText(), array(), array( 'redirect' => 'no' ) ) ) );
82 $this->showIndirectLinks( 0, $this->target
, $opts->getValue( 'limit' ),
83 $opts->getValue( 'from' ), $opts->getValue( 'back' ) );
87 * @param $level int Recursion level
88 * @param $target Title Target title
89 * @param $limit int Number of entries to display
90 * @param $from Title Display from this article ID
91 * @param $back Title Display from this article ID at backwards scrolling
94 function showIndirectLinks( $level, $target, $limit, $from = 0, $back = 0 ) {
95 global $wgMaxRedirectLinksRetrieved;
96 $out = $this->getOutput();
97 $dbr = wfGetDB( DB_SLAVE
);
100 $hidelinks = $this->opts
->getValue( 'hidelinks' );
101 $hideredirs = $this->opts
->getValue( 'hideredirs' );
102 $hidetrans = $this->opts
->getValue( 'hidetrans' );
103 $hideimages = $target->getNamespace() != NS_FILE ||
$this->opts
->getValue( 'hideimages' );
105 $fetchlinks = (!$hidelinks ||
!$hideredirs);
110 'pl_namespace' => $target->getNamespace(),
111 'pl_title' => $target->getDBkey(),
114 $plConds['page_is_redirect'] = 0;
115 } elseif( $hidelinks ) {
116 $plConds['page_is_redirect'] = 1;
121 'tl_namespace' => $target->getNamespace(),
122 'tl_title' => $target->getDBkey(),
127 'il_to' => $target->getDBkey(),
130 $namespace = $this->opts
->getValue( 'namespace' );
131 if ( is_int($namespace) ) {
132 $plConds['page_namespace'] = $namespace;
133 $tlConds['page_namespace'] = $namespace;
134 $ilConds['page_namespace'] = $namespace;
138 $tlConds[] = "tl_from >= $from";
139 $plConds[] = "pl_from >= $from";
140 $ilConds[] = "il_from >= $from";
143 // Read an extra row as an at-end check
144 $queryLimit = $limit +
1;
146 // Enforce join order, sometimes namespace selector may
147 // trigger filesorts which are far less efficient than scanning many entries
148 $options[] = 'STRAIGHT_JOIN';
150 $options['LIMIT'] = $queryLimit;
151 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect' );
154 $options['ORDER BY'] = 'pl_from';
155 $plRes = $dbr->select( array( 'pagelinks', 'page' ), $fields,
156 $plConds, __METHOD__
, $options );
160 $options['ORDER BY'] = 'tl_from';
161 $tlRes = $dbr->select( array( 'templatelinks', 'page' ), $fields,
162 $tlConds, __METHOD__
, $options );
166 $options['ORDER BY'] = 'il_from';
167 $ilRes = $dbr->select( array( 'imagelinks', 'page' ), $fields,
168 $ilConds, __METHOD__
, $options );
171 if( ( !$fetchlinks ||
!$dbr->numRows($plRes) ) && ( $hidetrans ||
!$dbr->numRows($tlRes) ) && ( $hideimages ||
!$dbr->numRows($ilRes) ) ) {
173 $out->addHTML( $this->whatlinkshereForm() );
175 // Show filters only if there are links
176 if( $hidelinks ||
$hidetrans ||
$hideredirs ||
$hideimages )
177 $out->addHTML( $this->getFilterPanel() );
179 $errMsg = is_int($namespace) ?
'nolinkshere-ns' : 'nolinkshere';
180 $out->addWikiMsg( $errMsg, $this->target
->getPrefixedText() );
185 // Read the rows into an array and remove duplicates
186 // templatelinks comes second so that the templatelinks row overwrites the
187 // pagelinks row, so we get (inclusion) rather than nothing
189 foreach ( $plRes as $row ) {
190 $row->is_template
= 0;
192 $rows[$row->page_id
] = $row;
196 foreach ( $tlRes as $row ) {
197 $row->is_template
= 1;
199 $rows[$row->page_id
] = $row;
203 foreach ( $ilRes as $row ) {
204 $row->is_template
= 0;
206 $rows[$row->page_id
] = $row;
210 // Sort by key and then change the keys to 0-based indices
212 $rows = array_values( $rows );
214 $numRows = count( $rows );
216 // Work out the start and end IDs, for prev/next links
217 if ( $numRows > $limit ) {
218 // More rows available after these ones
219 // Get the ID from the last row in the result set
220 $nextId = $rows[$limit]->page_id
;
221 // Remove undisplayed rows
222 $rows = array_slice( $rows, 0, $limit );
224 // No more rows after
230 $out->addHTML( $this->whatlinkshereForm() );
231 $out->addHTML( $this->getFilterPanel() );
232 $out->addWikiMsg( 'linkshere', $this->target
->getPrefixedText() );
234 $prevnext = $this->getPrevNext( $prevId, $nextId );
235 $out->addHTML( $prevnext );
238 $out->addHTML( $this->listStart( $level ) );
239 foreach ( $rows as $row ) {
240 $nt = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
242 if ( $row->page_is_redirect
&& $level < 2 ) {
243 $out->addHTML( $this->listItem( $row, $nt, true ) );
244 $this->showIndirectLinks( $level +
1, $nt, $wgMaxRedirectLinksRetrieved );
245 $out->addHTML( Xml
::closeElement( 'li' ) );
247 $out->addHTML( $this->listItem( $row, $nt ) );
251 $out->addHTML( $this->listEnd() );
254 $out->addHTML( $prevnext );
258 protected function listStart( $level ) {
259 return Xml
::openElement( 'ul', ( $level ?
array() : array( 'id' => 'mw-whatlinkshere-list' ) ) );
262 protected function listItem( $row, $nt, $notClose = false ) {
263 # local message cache
264 static $msgcache = null;
265 if ( $msgcache === null ) {
266 static $msgs = array( 'isredirect', 'istemplate', 'semicolon-separator',
267 'whatlinkshere-links', 'isimage' );
269 foreach ( $msgs as $msg ) {
270 $msgcache[$msg] = wfMsgExt( $msg, array( 'escapenoentities' ) );
274 if( $row->page_is_redirect
) {
275 $query = array( 'redirect' => 'no' );
280 $link = Linker
::linkKnown(
287 // Display properties (redirect or template)
290 if ( $row->page_is_redirect
)
291 $props[] = $msgcache['isredirect'];
292 if ( $row->is_template
)
293 $props[] = $msgcache['istemplate'];
295 $props[] = $msgcache['isimage'];
297 if ( count( $props ) ) {
298 $propsText = '(' . implode( $msgcache['semicolon-separator'], $props ) . ')';
301 # Space for utilities links, with a what-links-here link provided
302 $wlhLink = $this->wlhLink( $nt, $msgcache['whatlinkshere-links'] );
303 $wlh = Xml
::wrapClass( "($wlhLink)", 'mw-whatlinkshere-tools' );
306 Xml
::openElement( 'li' ) . "$link $propsText $wlh\n" :
307 Xml
::tags( 'li', null, "$link $propsText $wlh" ) . "\n";
310 protected function listEnd() {
311 return Xml
::closeElement( 'ul' );
314 protected function wlhLink( Title
$target, $text ) {
315 static $title = null;
316 if ( $title === null )
317 $title = $this->getTitle();
319 return Linker
::linkKnown(
323 array( 'target' => $target->getPrefixedText() )
327 function makeSelfLink( $text, $query ) {
328 return Linker
::linkKnown(
336 function getPrevNext( $prevId, $nextId ) {
338 $currentLimit = $this->opts
->getValue( 'limit' );
339 $fmtLimit = $wgLang->formatNum( $currentLimit );
340 $prev = wfMsgExt( 'whatlinkshere-prev', array( 'parsemag', 'escape' ), $fmtLimit );
341 $next = wfMsgExt( 'whatlinkshere-next', array( 'parsemag', 'escape' ), $fmtLimit );
343 $changed = $this->opts
->getChangedValues();
344 unset($changed['target']); // Already in the request title
346 if ( 0 != $prevId ) {
347 $overrides = array( 'from' => $this->opts
->getValue( 'back' ) );
348 $prev = $this->makeSelfLink( $prev, array_merge( $changed, $overrides ) );
350 if ( 0 != $nextId ) {
351 $overrides = array( 'from' => $nextId, 'back' => $prevId );
352 $next = $this->makeSelfLink( $next, array_merge( $changed, $overrides ) );
355 $limitLinks = array();
356 foreach ( $this->limits
as $limit ) {
357 $prettyLimit = $wgLang->formatNum( $limit );
358 $overrides = array( 'limit' => $limit );
359 $limitLinks[] = $this->makeSelfLink( $prettyLimit, array_merge( $changed, $overrides ) );
362 $nums = $wgLang->pipeList( $limitLinks );
364 return wfMsgHtml( 'viewprevnext', $prev, $next, $nums );
367 function whatlinkshereForm() {
370 // We get nicer value from the title object
371 $this->opts
->consumeValue( 'target' );
372 // Reset these for new requests
373 $this->opts
->consumeValues( array( 'back', 'from' ) );
375 $target = $this->target ?
$this->target
->getPrefixedText() : '';
376 $namespace = $this->opts
->consumeValue( 'namespace' );
379 $f = Xml
::openElement( 'form', array( 'action' => $wgScript ) );
381 # Values that should not be forgotten
382 $f .= Html
::hidden( 'title', $this->getTitle()->getPrefixedText() );
383 foreach ( $this->opts
->getUnconsumedValues() as $name => $value ) {
384 $f .= Html
::hidden( $name, $value );
387 $f .= Xml
::fieldset( wfMsg( 'whatlinkshere' ) );
390 $f .= Xml
::inputLabel( wfMsg( 'whatlinkshere-page' ), 'target',
391 'mw-whatlinkshere-target', 40, $target );
396 $f .= Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
397 Xml
::namespaceSelector( $namespace, '' );
402 $f .= Xml
::submitButton( wfMsg( 'allpagessubmit' ) );
405 $f .= Xml
::closeElement( 'fieldset' ) . Xml
::closeElement( 'form' ) . "\n";
411 * Create filter panel
413 * @return string HTML fieldset and filter panel with the show/hide links
415 function getFilterPanel() {
417 $show = wfMsgHtml( 'show' );
418 $hide = wfMsgHtml( 'hide' );
420 $changed = $this->opts
->getChangedValues();
421 unset($changed['target']); // Already in the request title
424 $types = array( 'hidetrans', 'hidelinks', 'hideredirs' );
425 if( $this->target
->getNamespace() == NS_FILE
)
426 $types[] = 'hideimages';
428 // Combined message keys: 'whatlinkshere-hideredirs', 'whatlinkshere-hidetrans', 'whatlinkshere-hidelinks', 'whatlinkshere-hideimages'
429 // To be sure they will be find by grep
430 foreach( $types as $type ) {
431 $chosen = $this->opts
->getValue( $type );
432 $msg = $chosen ?
$show : $hide;
433 $overrides = array( $type => !$chosen );
434 $links[] = wfMsgHtml( "whatlinkshere-{$type}", $this->makeSelfLink( $msg, array_merge( $changed, $overrides ) ) );
436 return Xml
::fieldset( wfMsg( 'whatlinkshere-filters' ), $wgLang->pipeList( $links ) );